import java.util.Arrays;

public class PriorityQueue {
    public static int[] elem;
    public static int usedSize;

    public PriorityQueue() {
        this.elem = new int[10];
    }

    /**
     * 建堆的时间复杂度：o[n]
     *
     * @param array
     */
    public void createHeap(int[] array) {
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }

    /**
     *
     * @param parent 是每棵子树的根节点的下标
     * @param usedSize  是每棵子树调整结束的结束条件
     * 向下调整的时间复杂度：O(log2n)
     */
    public static void shiftDown(int parent,int usedSize) {
        int child = parent * 2 + 1;
        while(child < usedSize){
            //找到最小孩子结点
            if (child + 1 < usedSize && elem[child] > elem[child+1]){
                child++;
            }
            //交换
            if(elem[parent] > elem[child]){
             int tmp = elem[parent];
             elem[parent] = elem[child];
             elem[child] = tmp;
             parent = child;
             child = 2*parent+1;
            }else{
                break;
            }
        }
    }

    /**
     * 入队：仍然要保持是小根堆
     * @param val
     */
    public static void push(int val) {
        if (isFull()){
            //扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        //插到最后一个结点
        elem[usedSize] = val;
        usedSize++;

        int child = usedSize-1;
        int parent = (child-1)/2;
        while (parent >=0){
            if(elem[parent] > elem[child]){
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                child = parent;
                parent = ( parent-1) / 2;
            }else{
                break;
            }
        }
    }
    //向上调整
    public static void shiftUp(int child) {
        int parent = (child - 1) / 2;
        while(parent >= 0){
            //找到最小孩子结点
            if (child + 1 < usedSize && elem[child] > elem[child+1]){
                child++;
            }
            //交换
            if(elem[parent] > elem[child]){
                int tmp = elem[parent];
                elem[parent] = elem[child];
                elem[child] = tmp;
                child = parent;
                parent = ( parent-1) / 2;
            }else{
                break;
            }


        }

    }

    public static boolean isFull() {
        return elem.length == usedSize;
    }


    /**
     * 出队【删除】：每次删除的都是优先级高的元素
     * 仍然要保持是小根堆
     */
    public static void pollHeap() {
        int parent = 0;
        int tmp = elem[parent];
        elem[parent] = elem[usedSize-1];
        elem[usedSize-1] = tmp ;
        usedSize--;
        shiftDown(0,usedSize);
    }

    public boolean isEmpty() {
        return  usedSize == 0;
    }

    /**
     * 获取堆顶元素
     * @return
     */
    public int peekHeap() {
        return elem[0];
    }


    //利用向下调整，创建堆
    public static void sort(){
        int parent = 0;
        while(parent < usedSize){
            shiftDown(parent,usedSize);
            parent = parent*2+1;
        }
    }

    //用小根堆进行降序排列
    public static void sort1(){
        int end = usedSize -1;
        while(end > 0){
            int tmp = elem[0];
            elem[0] = elem[end];
            elem[end] = tmp;
            end--;
            shiftDown(0,end);
         }
    }
    //用删除操作排序
    public static void sort2(){
        int end = usedSize - 1;
        while(end > 0){
            pollHeap();
            end--;
        }

    }
}
